import axios from "axios";
import useData from "@/store";

const instance = axios.create({
  // baseURL: "http://biubiu0002.tpddns.cn:8081",
  // baseURL: "http://124.221.113.22:9004",
  baseURL: "http://124.221.113.22",
  timeout: 10000,
});

export const get = async (url: string, params = {}) => {
  const headers = {
    "x-auth-token": useData.getState().userInfo?.token,
  };

  return new Promise<{ data: any }>((resolve, reject) => {
    instance.get(url, { params, headers }).then((res) => {
      if (res.data.statusCode === 200) {
        resolve(res.data);
      } else {
        reject(res.data);
      }
    });
  });
};

export const post = async (url: string, data = {}) => {
  const headers = {
    "x-auth-token": useData.getState().userInfo?.token,
  };

  return new Promise<{ data: any }>((resolve, reject) => {
    instance.post(url, data, { headers }).then((res) => {
      if (res.data.statusCode === 200) {
        resolve(res.data);
      } else {
        reject(res.data);
      }
    });
  });
};

export default {
  get,
  post,
};
